package org.dromara.oce.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytedance.ads.model.ToolsSiteGetV2ResponseData;
import com.bytedance.ads.model.ToolsSiteGetV2ResponseDataListInner;
import com.bytedance.ads.model.ToolsSiteGetV2ShareType;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.oce.domain.OceSite;
import org.dromara.oce.domain.bo.OceSiteBo;
import org.dromara.oce.domain.qo.OceSiteQo;
import org.dromara.oce.domain.req.SyncSiteReq;
import org.dromara.oce.domain.req.ToolsSiteListV2Request;
import org.dromara.oce.domain.vo.OceSiteVo;
import org.dromara.oce.mapper.OceSiteMapper;
import org.dromara.oce.service.OceSiteService;
import org.dromara.oce.service.remote.OceanEngineThirdRemoteService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 橙子建站落地页Service业务层处理
 *
 * @author dsbb
 * @date 2025-06-10
 */
@RequiredArgsConstructor
@Service
public class OceSiteServiceImpl extends ServiceImpl<OceSiteMapper, OceSite> implements OceSiteService {

    private final OceSiteMapper baseMapper;
    private final OceanEngineThirdRemoteService oceanEngineThirdRemoteService;

    /**
     * 查询橙子建站落地页
     *
     * @param siteId 主键
     * @return 橙子建站落地页
     */
    @Override
    public OceSiteVo queryById(Long siteId) {
        return baseMapper.selectVoById(siteId);
    }

    /**
     * 分页查询橙子建站落地页列表
     *
     * @param qo        查询条件
     * @param pageQuery 分页参数
     * @return 橙子建站落地页分页列表
     */
    @Override
    public TableDataInfo<OceSiteVo> queryPageList(OceSiteQo qo, PageQuery pageQuery) {
        LambdaQueryWrapper<OceSite> lqw = buildQueryWrapper(qo);
        Page<OceSiteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的橙子建站落地页列表
     *
     * @param qo 查询条件
     * @return 橙子建站落地页列表
     */
    @Override
    public List<OceSiteVo> queryList(OceSiteQo qo) {
        LambdaQueryWrapper<OceSite> lqw = buildQueryWrapper(qo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OceSite> buildQueryWrapper(OceSiteQo qo) {
        LambdaQueryWrapper<OceSite> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增橙子建站落地页
     *
     * @param bo 橙子建站落地页
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OceSiteBo bo) {
        OceSite add = MapstructUtils.convert(bo, OceSite.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSiteId(add.getSiteId());
        }
        return flag;
    }

    /**
     * 修改橙子建站落地页
     *
     * @param bo 橙子建站落地页
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OceSiteBo bo) {
        OceSite update = MapstructUtils.convert(bo, OceSite.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OceSite entity) {
    }

    /**
     * 校验并批量删除橙子建站落地页信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void syncSite(SyncSiteReq req) {
        ToolsSiteListV2Request request = new ToolsSiteListV2Request();
        BeanUtil.copyProperties(req, request);
        request.setShareType(ToolsSiteGetV2ShareType.MY_CREATIONS);
        request.setPageNum(1L);
        request.setPageSize(300L);
        ToolsSiteGetV2ResponseData toolsSiteList = oceanEngineThirdRemoteService.getToolsSiteList(req.getAccountId(), request);

        List<String> existSiteIdList = this.list(new LambdaQueryWrapper<OceSite>().eq(OceSite::getAdvertiserId, req.getAdvertiserId())).stream().map(item -> String.valueOf(item.getSiteId())).collect(Collectors.toList());
        List<OceSite> addList = new ArrayList<>();
        for (ToolsSiteGetV2ResponseDataListInner inner : toolsSiteList.getList()) {
            if (existSiteIdList.contains(inner.getSiteId())) {
                continue;
            }
            OceSite oceSite = BeanUtil.copyProperties(inner, OceSite.class);
            oceSite.setAccountId(req.getAccountId());
            oceSite.setAdvertiserId(req.getAdvertiserId());
            addList.add(oceSite);
        }
        if (addList.size() > 0) {
            this.saveBatch(addList);
        }
    }
}
